package com.eianiao.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eianiao.bean.Employee;
import com.eianiao.common.PageParam;
import com.eianiao.common.R;
import com.eianiao.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @Author eianiao
 * @Date 2022 06
 * @Version 1.0
 **/
//处理一切与员工相关的请求
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService es;

    /**
     * 登录界面
     *
     * @param employee
     * @param session
     * @return
     * @RequestBody让对象接收Json对象
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) {

        Employee employee1 = es.login(employee);
        session.setAttribute("employee", employee1);
        if (employee1 != null) {
            return R.success(employee1);
        }
        return R.error("用户名或者密码错误！");
    }


    /**
     * 退出界面
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        try {
            session.invalidate();
            return R.success("退出成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败！");
        }
    }


    /**
     * 添加员工信息
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee, HttpSession session) {
        //0. 补充数据
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes())); // 设置初始密码： 123456
        employee.setStatus(1); // 设置状态： 1表示启用， 0 表示禁用
 /*       employee.setCreateTime(LocalDateTime.now()); // 设置创建时间
        employee.setUpdateTime(LocalDateTime.now()); //设置更新时间
        Employee e = (Employee) session.getAttribute("employee");
        employee.setCreateUser(e.getId()); // 设置创建的用户
        employee.setUpdateUser(e.getId()); // 设置更新的用户*/
        //1.添加员工
        int rows = es.add(employee);
        if (rows > 0) {
            return R.success("添加成功！");
        } else {
            return R.error("添加失败！");
        }

}

    /**
     * 更新员工信息
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee, HttpSession session){
  /*      //设置什么时候修改的数据
        employee.setUpdateTime(LocalDateTime.now());
        //设置是谁修改的信息
        Employee e = (Employee) session.getAttribute("employee");
        employee.setUpdateUser(e.getId());*/


        int row = es.update(employee);
        if (row > 0) {
            return R.success("更新成功！");
        } else {
            return R.error("更新失败！");
        }
    }

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<Employee> page = es.findPage(pageParam);

        return R.success(page);
    }

}